package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 虎哥
 */
@RestController
@RequestMapping("spec")
public class SpecController {

    private SpecGroupService groupService;
    private SpecParamService paramService;

    public SpecController(SpecGroupService groupService, SpecParamService paramService) {
        this.groupService = groupService;
        this.paramService = paramService;
    }

    /**
     * 根据分类id查询规格组
     *
     * @param categoryId 分类的id
     * @return 规格组的集合
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategoryId(@RequestParam("id") Long categoryId) {
        List<SpecGroupDTO> list = groupService.queryGroupByCategoryId(categoryId);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据分类id查询规格组及组内参数
     * @param categoryId 分类id
     * @return 规格组及组内参数集合
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id") Long categoryId){
        List<SpecGroupDTO> list = groupService.querySpecList(categoryId);
        return ResponseEntity.ok(list);
    }

    /**
     * 查询规格参数
     *
     * @param categoryId 分类id
     * @param groupId    规格组id
     * @param searching  是否参与搜索
     * @return 参数的集合
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) Boolean searching
    ) {
        // 查询
        List<SpecParamDTO> list = paramService.queryParams(categoryId, groupId, searching);
        // 返回
        return ResponseEntity.ok(list);
    }

}
